package cn.netswap.jeegorilla.framework.security.service;

import cn.netswap.jeegorilla.framework.security.model.SysPermission;
import cn.netswap.jeegorilla.framework.security.model.SysUser;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.social.security.SocialUser;
import org.springframework.social.security.SocialUserDetails;
import org.springframework.social.security.SocialUserDetailsService;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
public class GorillaUserDetailsService implements UserDetailsService, SocialUserDetailsService {
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 模拟根据用户名查询用户信息和权限
        SysUser user = null;
        if(username.equals("13719324114")){
             user = new SysUser(1L, username, "$2a$10$nm5H9QvnoWao.l7NbxQGZeZoR0Cn.VqCpsl3E/FhglPa954Zg9ccm",
                    Arrays.asList(new SysPermission(1L, "userlist", "user:view", "/getUserList", "GET"),
                            new SysPermission(2L, "adduser", "user:add", "/addUser", "POST"),
                            new SysPermission(3L, "modefily", "user:update", "/updateUser", "PUT")
                    ));
        }
        if (user == null) {
           throw  new UsernameNotFoundException("登录失败");
        }

        return new User(user.getUsername(), user.getPassword(), user.getSysPermissions());
    }


    public static void main(String[] args) {
        System.out.println(new BCryptPasswordEncoder().encode("123456"));
    }

    @Override
    public SocialUserDetails loadUserByUserId(String userId) throws UsernameNotFoundException {
        // 根据用户名查询用户信息和权限
        SysUser user = queryUserInfo(null, userId);
        if (user == null) {
            throw new UsernameNotFoundException(userId);
        }

        return new SocialUser(userId, user.getPassword(), true, true, true, true, user.getSysPermissions());
    }

    public SysUser queryUserInfo(String username, String userId) {
        SysUser user = new SysUser(1L, "admin", "$2a$10$nm5H9QvnoWao.l7NbxQGZeZoR0Cn.VqCpsl3E/FhglPa954Zg9ccm", Arrays.asList(
                new SysPermission(1L, "用户列表", "user:view", "/getUserList", "GET"),
                new SysPermission(2L, "添加用户", "user:add", "/addUser", "POST"),
                new SysPermission(3L, "修改用户", "user:update", "/updateUser", "PUT")
        ));

        return user;
    }


}
